'''MarysTicTacToe.py
test program for harness.

'''

mK = 3
def myNameIs():
    return "Samrach"

def myCreatorIs():
    return "Samrach Nouv"

def prepare(k, mRows, nColumns, maxMillisecPerMove, isPlayingX, debugging):
    if (k > max(mRows, nColumns)):
        return "Let's quit right now because nobody can get %d in a row on a %d by %d board" % (k, mRows, mColumns)
    mK=k
    return "OK"
        
def introduce():
    return '%s\n%s\nharmless joker' % (nickname(), myCreatorIs())
    
def nickname():
    return "sam"

def successors(currentState):
    mySide = currentState[0]
    board = currentState[1]
    childrenState = []
    tempState = []
    for rowInd in range(len(board)):
        for colInd in range(len(board[rowInd])):
            if board[rowInd][colInd] == ' ':
                tempState = board[:]
                tempState[rowInd][colInd]=mySide
                childrenState.append([other(mySide),tempState])
                return childrenState

def makeMove(currentState):
    mySide = currentState[0][:]
    successors = successors(currentState[:])
    if (successors==[]):
        return [currentState, "I could not find any legal move."]
    newState = successors[0][:]
    return [newState, emitUtterance(newState)]

#returns the value of the static evaluation function on the given state
def staticEval(state):
    global mK
    mySide = other(state[0])
    board = state[1][:]
    myVal = [0]*(mK+1)
    otherVal = [0]*(mK+1)
    # add all rows to be checked
    dataToBeChecked = board[:]
    # add all cols to be checked
    dataToBeChecked.extend(selectColumns(board[:]))
    # add all diagonals to be checked
    dataToBeChecked.extend(selectDiagonals(board[:]))
    for data in dataToBeChecked:
        myCount = count(mySide, data)
        otherCount = count(other(mySide), data)
        myVal[myCount]+= myCount
        otherVal[otherCount]+=otherCount
    print myVal
    print otherVal
    return calculateScore(myVal, otherVal)
    
def calculateScore(myCounts, otherCounts):
    mySum = 0
    otherSum = 0
    mk = len(myCounts)
    for i in range(1, mk):
        print "pow(10,%d)=%d and myCount =%d" % (i-1, pow(10,i-1), myCounts[i])
        mySum+=pow(10,i-1)*myCounts[i]
        otherSum+=pow(10,i-1)*otherCounts[i]
    return mySum - otherSum
        

#helper function count occurence of a symbol in a given list
def count(symbol, l):
   sum=0
   for s in l:
      if s!=' ':
         if s!=symbol: return 0
         else: sum+=1
   return sum
            
def other(player):
    if player=='X': return 'O'
    else: return 'X'

'''***************need to be fix***********************'''
def emitUtterance(currentState):
    return "don't know yet"



def selectColumns(m):
    lst = m[:]
    result =[]
    cols = len(lst[0])
    for col in range(cols):
        tmp=[]
        for row in range(len(lst)):
            tmp.append(lst[row][col])
        result.append(tmp)
    return result

def selectDiagonals(m):
    lst = m[:]
    result =[]
    col_len = len(lst[0])
    row_len = len(lst)
    
    for row in range(row_len-2, -1, -1):
        row_counter = row
        tmp = []
        col_counter = 0
        while ( row_counter <= row_len-1 and col_counter <= col_len - 1):
            tmp.append(lst[row_counter][col_counter])
            row_counter = row_counter + 1
            col_counter = col_counter + 1
        result.append(tmp)
    for col in range(1, col_len-1):
        col_counter = col;
        tmp = []
        row_counter = 0;
        while ( row_counter <= row_len-1 and col_counter <= col_len - 1):
            tmp.append(lst[row_counter][col_counter])
            row_counter = row_counter + 1
            col_counter = col_counter + 1
        result.append(tmp)

    for row in range(1, row_len):
        row_counter = row
        tmp = []
        col_counter = 0
        while ( row_counter >=0 and col_counter <= col_len - 1):
            tmp.append(lst[row_counter][col_counter])
            row_counter = row_counter - 1
            col_counter = col_counter + 1
        result.append(tmp)
    
    for col in range(1, col_len-1):
        col_counter = col;
        tmp = []
        row_counter = row_len-1;
        while ( row_counter >= 0 and col_counter <= col_len - 1):
            tmp.append(lst[row_counter][col_counter])
            row_counter = row_counter - 1
            col_counter = col_counter + 1
        result.append(tmp)
    return result
            
